package com.example.demo.controllers;

import com.example.demo.form.FileAndSpecialInformationForm;
import com.example.demo.service.IStorageService;
import com.example.demo.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/")

public class imageController {
    @Autowired
    private IStorageService storageService;

    @Value("${upload.accessPath}")
    private String accessPath;

    @Value("${upload.localPath}")
    private String localPath;
    /*
    获得用户特殊信息与文件名字进行拼接并返回
     */
    @PostMapping("/upload")
    public Result upload(HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");
        String fileName = file.getOriginalFilename();
        log.info("fileName: {}", fileName);
        storageService.save(file, fileName, localPath);
        return Result.success(accessPath + fileName);
    }
}
